import { View, Text } from '@tarojs/components'
import Taro, { Current } from '@tarojs/taro'
import { useState, useEffect, useMemo } from 'react'
import Loading from '@/myComponent/load'
import User from '../user'
import Admin from '../admin'
import Utils from '@/utils/index'
import { getStorage } from '@/pages/components/utils'
import { useDispatch } from 'react-redux'
import { setAdvanced, setIdentityCode, setSysInfo, setToken, setType, setUserInfo } from '@/store/slice'
import { InitParams } from './type'
import { requestSystemUrl, getBaseUrl } from '@/network/baseUrl'
import { HttpGet } from '@/network/servers'
import './index.scss'
import { useOnlineCheck } from '@/utils/myUtils'

const storage = getStorage()

export default function App() {

    const dispatch = useDispatch()
    const [init, setInit] = useState(false) //初始化
    const [pageType, setPageType] = useState<string | boolean>('user')

    const body = useMemo(() => {
        if (pageType === 'admin') {
            return <Admin />
        } else {
            return <User />
        }
    }, [pageType])

    const getCommonData = (params: InitParams) => {
        HttpGet({ //用户信息
            url: params.baseUrl + '/api/common/user',
            data: {
                token: params.lg_tk
            }
        }).then((userInfo) => {
            dispatch(setUserInfo(userInfo))
            let identityList=userInfo.identityList || []
            let currentIdentity=identityList.find(i=>i.identityCode===params.lg_ic) || {}
            
            // if(currentIdentity.advanced&&currentIdentity.advanced==1){ //当前身份内是否有超级管理员权限
            if (Utils.getQueryVariable('type') == 'admin'){
                dispatch(setAdvanced(1))
            }
            HttpGet({ //学校信息
                url: params.baseUrl+'/api/common/system/info',
                data: {
                    eduId: userInfo.eduId,
                    token: params.lg_tk
                }
            }).then((sysInfo) => {
                dispatch(setSysInfo(sysInfo))
                HttpGet({ //锁校验
                    url: '/api/commons/locker/check',
                    data:{
                        sysId:sysInfo.noticeSysId,
                        lockerId:sysInfo.noticeLockerId,
                        productUseRange:sysInfo.productUseRange,
                        schoolId:sysInfo.schoolId
                    }
                }).then((lock) => {
                    if (lock.result == 1) {
                        useOnlineCheck(sysInfo.baseUrl,params.lg_tk,sysInfo.noticeSysId)
                        setInit(true)
                    } else {
                        let url = lock.lockErrAddr + lock.result
                        window.open(url, '_self')
                    }
                })
            })
        })
    }

    useEffect(() => {
        let type = Current.router?.params.type || Utils.getQueryVariable('type')
        setPageType(type)
        dispatch(setType(type))

        let params: InitParams = {
            lg_tk:storage.get('token') || Utils.getQueryVariable('lg_tk')  ,
            lg_ic:Utils.getQueryVariable('lg_ic') || storage.get('lg_ic'),
            baseUrl: getBaseUrl()
        }

        dispatch(setToken(params.lg_tk))
        dispatch(setIdentityCode(params.lg_ic))

        if (Utils.getSystemInfo() === 'weapp') {
            requestSystemUrl('222').then((baseUrl) => {
                params = Object.assign({}, params, { baseUrl })
                window.baseUrl=baseUrl
                getCommonData(params)
            })
        } else {
            getCommonData(params)
        }
    }, [])

    if (!init) {
        return <Loading />
    }

    return (
        <View className='announcemnet_app'>
            {body}
        </View>
    )
}